package com.sino.wolf.app.controller;

import com.github.pagehelper.PageHelper;
import com.sino.wolf.app.entity.MyUser;
import com.sino.wolf.app.service.MyUserService;
import com.sino.wolf.common.helper.MessageHelper;
import com.sino.wolf.common.page.PageBean;
import com.sino.wolf.common.page.PageModel;
import com.sino.wolf.common.page.ResponseResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("/user")
public class MyUserController {

    @Autowired
    private MyUserService myUserService;

    @RequestMapping(value = "/register", method = RequestMethod.POST)
    public String register(HttpServletRequest request) {
        try {
            String userName = request.getParameter("username");
            String password = request.getParameter("password");

            MyUser user = new MyUser();
            user.setUserName(userName);
            user.setPassword(password);
            return myUserService.insertUser(user)+"";
        } catch (Exception e) {
            return e.getMessage();
        }
    }

    @RequestMapping("/findAllUser")
    public ResponseResult<PageBean> findAllUser(PageModel pageModel) {
        ResponseResult<PageBean> responseResult;
        try {
            PageHelper.startPage(pageModel.getPageNum(), pageModel.getPageSize());
            PageHelper.orderBy("id asc");
            List<MyUser> myUsers = myUserService.selectUser();
            return MessageHelper.successPage(myUsers);
        } catch (Exception e) {
            e.printStackTrace();
            return MessageHelper.failurePage();
        }
    }
}